package de.hoits.app.gewichtskontolle;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphView.GraphViewSeries;
import com.jjoe64.graphview.LineGraphView;

public class GewichtsKontrolleActivity extends Activity
{

	private int myYear;
	private int myMonth;
	private int myDay;

	static final int DATE_DIALOG_ID = 0;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(final Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// fuelleDatumsFeld();

		final GraphViewSeries exampleSeries = new GraphViewSeries(new GraphViewData[] {
				new GraphViewData(0, 42.0d), new GraphViewData(1, 2.0d),
				new GraphViewData(2, 1.5d),
				new GraphViewData(2.5, 3.0d) // another frequency
				, new GraphViewData(3, 2.5d), new GraphViewData(4, 1.0d),
				new GraphViewData(5, 3.0d) });

		// final graph with dynamically final genereated horizontal and final
		// vertical labels
		final GraphView graphView = new LineGraphView(this // context
				, "GraphViewDemo" // heading
		);

		graphView.addSeries(exampleSeries); // data

		final LinearLayout layout = (LinearLayout) findViewById(R.id.graph);
		layout.addView(graphView);

		final Button mPickDate = (Button) findViewById(R.id.button1);

		// add a click listener to the button
		mPickDate.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(final View v)
			{
				showDialog(DATE_DIALOG_ID);
			}
		});

		// get the current date
		final Calendar c = Calendar.getInstance();
		myYear = c.get(Calendar.YEAR);
		myMonth = c.get(Calendar.MONTH);
		myDay = c.get(Calendar.DAY_OF_MONTH);

		// display the current date (this method is below)
		updateDisplay();

	}

	@Override
	protected Dialog onCreateDialog(final int id)
	{
		switch (id)
		{
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListener, myYear, myMonth, myDay);
		}
		return null;
	}

	// the callback received when the user "sets" the date in the dialog
	private final DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
	{

		@Override
		public void onDateSet(final DatePicker view, final int year, final int monthOfYear,
				final int dayOfMonth)
		{
			myYear = year;
			myMonth = monthOfYear;
			myDay = dayOfMonth;
			updateDisplay();
		}
	};

	// updates the date in the TextView
	private void updateDisplay()
	{
		final EditText mDateDisplay = (EditText) findViewById(R.id.eingabeDatum);
		mDateDisplay.setText(new StringBuilder()
				// Month is 0 based so add 1
				.append(myMonth + 1).append("-").append(myDay).append("-").append(myYear)
				.append(" "));
	}

	/**********************************************************************/

	/**
	 * erzeugt das heutige Datum in das Eingabefeld
	 */
	@SuppressWarnings("unused")
	private void fuelleDatumsFeld()
	{
		final EditText eingabeDatum = (EditText) findViewById(R.id.eingabeDatum);
		final SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
		eingabeDatum.setText(sdf.format(new Date()));
	}
}